package zhengfuzhe.com.newupload1.other;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;

import zhengfuzhe.com.newupload1.bean.LocalAppInfoBean;
import zhengfuzhe.com.newupload1.bean.NUDBean;
import zhengfuzhe.com.newupload1.bean.NetAppInfoBean;

public class MatchAppUtil {
    private Context mContext;

    private MatchAppUtil(Context context) {
        this.mContext = context;
    }

    private static MatchAppUtil instance = null;

    public static MatchAppUtil getInstance(Context context) {

        if (instance == null) {
            synchronized (MatchAppUtil.class) {
                if (instance == null) {
                    instance = new MatchAppUtil(context);
                }
            }
        }

        return instance;
    }

    /**
     * 获取需要更新的app
     *
     * @param localList(本地的app)
     * @param networkList(服务器上面的app)
     * @return ArrayList<NetAppInfoBean.ListBean>(需要更新的app)
     */
    public ArrayList<NUDBean> getNeedUpDateApp(ArrayList<LocalAppInfoBean> localList, ArrayList<NetAppInfoBean.ListBean> networkList) {
        ArrayList<NUDBean> bean = new ArrayList<>();
        for (int i = 0; i < networkList.size(); i++) {//遍历所有的网络应用

            NetAppInfoBean.ListBean netAppInfoBean = networkList.get(i);

            if (!isInstalled(netAppInfoBean.packageName)) {//如果本地没有那就是新增的，直接存储起来
                Log.e("MyLog", "----netAppInfoBean.packageName----" + netAppInfoBean.packageName);
                NUDBean nudBean = new NUDBean();
                nudBean.appName = netAppInfoBean.fileName;
                nudBean.versionName = netAppInfoBean.versionName;
                nudBean.versionCode = netAppInfoBean.versionCode;
                nudBean.des = netAppInfoBean.updateMess;
                nudBean.packageName = netAppInfoBean.packageName;
                nudBean.url = netAppInfoBean.url;
                bean.add(nudBean);
            }

            for (int j = 0; j < localList.size(); j++) {//遍历所有的本地应用
                LocalAppInfoBean localAppInfoBean = localList.get(j);
                if (netAppInfoBean.packageName.equals(localAppInfoBean.packageName)) {//如果网络应用和本地应用同名
                    if (localAppInfoBean.versionCode < netAppInfoBean.versionCode) {//本地版本号比网络版本号小，说明需要更新
                        NUDBean nudBean = new NUDBean();
                        nudBean.appName = netAppInfoBean.fileName;
                        nudBean.versionName = netAppInfoBean.versionName;
                        nudBean.versionCode = netAppInfoBean.versionCode;
                        nudBean.des = netAppInfoBean.updateMess;
                        nudBean.url = netAppInfoBean.url;
                        nudBean.packageName = netAppInfoBean.packageName;
                        bean.add(nudBean);
                    }
                }
            }
        }

        Log.e("MyLog", "-----bean------" + bean);
        return bean;//就是需要显示的数据
    }


    /**
     * 通过包名检测APP是否安装
     *
     * @param packageName 包名
     * @return true or false
     */
    public boolean isInstalled(String packageName) {
        boolean isInstalled = false;
        if (!TextUtils.isEmpty(packageName)) {
            PackageInfo packageInfo;
            try {
                packageInfo = mContext.getPackageManager().getPackageInfo(packageName, 0);
            } catch (PackageManager.NameNotFoundException e) {
                packageInfo = null;
                e.printStackTrace();
            }
            if (packageInfo == null) {
                isInstalled = false;
            } else {
                isInstalled = true;
            }
        }

        Log.e("MyLog", "----installed----" + isInstalled);
        return isInstalled;
    }

}
